import Base from '../../base.js'
export default class student extends Base {
    render() {
        $('#content').html(`
        <div style="margin: 30px 10px 0 20px " id='nav'>
        <span class="layui-breadcrumb">
            <a>种猪培训班</a>
            <a>种猪一班</a>
            <a>种猪列表</a>
         </span>
         <hr/>
        </div>
        <h1  style="margin: 10px 10px 0 20px ">种猪列表</h1>
        <hr/>
        <button id='addBtn' style="margin: 10px 0px 15px 15px" class='layui-btn layui-btn-normal'>
        <i class="layui-icon layui-icon-add-1"></i>添加
        </button> 
        <table id="showDatas"></table>
        `)
    }
    handle() {
        $("script[type='module']").append(`

        //绑定工具栏
        <script type="text/html" id="barDemo" >
          <a class="layui-btn layui-btn-xs" lay-event="detail">查询</a>
          <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>


        <script>
       layui.use('table', function () {
    var table = layui.table;

    table.render({
        elem: '#showDatas',
        url: '/api/student/show',
        parseData: function (res) {
            return {
                "code": res.status, //解析接口状态
                "msg": res.message, //解析提示文本
                "count": res.total, //解析数据长度
                "data": res.datas //解析数据列表
            }
        },
        title: '用户数据表',
        cols: [
            [ //表头
                {
                    field: '_id',
                    title: '编号',
                    fixed: 'left'
                }, {
                    field: 'name',
                    title: '用户名',
                }, {
                    field: 'gender',
                    title: '性别'
                }, {
                    field: 'age',
                    title: '城市',
                }, {
                    field: 'address',
                    title: '签名',
                }, {
                    field: 'score',
                    title: '评分',
                }, {
                    fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo'
                }
            ]
        ]
    });

    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        console.log(obj)
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.prompt({
                formType: 2,
                value: data.email
            }, function (value, index) {
                obj.update({
                    email: value
                });
                layer.close(index);
            });
        }
    });
});

        </script>

      `)

        $('#addBtn').click(() => {
            window.location.hash = '#/Main/addstudent'
        })

    }
}